<?php

defined('BASEPATH') or die('forbidden');

class CdkeyType_Controller Extends Controller {

    private $inc;
    private $center_connect;

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));

        $this->inc = $this->config['inc'];
        $this->center_connect = $this->load->database('center_db');
    }

    function index() {
        $this->view->display("freegift/cdkeytype/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM cdkey_type WHERE {$where}";
        $one = $this->center_connect->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $sql = "SELECT {$colsinfo} FROM cdkey_type WHERE {$where} {$orderby} LIMIT {$offset},{$rp}";
        $this->center_connect->query($sql);

        while ($n = $this->center_connect->get_array('default')) {
            $rows[] = array('id' => $n['type'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];

            $one = $this->center_connect->one("SELECT type FROM  cdkey_type WHERE type='{$rowdata['type']}'");
            if (is_array($one))
                error(translate('C_EXISTED_PARAM_ID', $rowdata['type'] . ':' . $rowdata['name']));
            //更新数据表
            $id = $this->center_connect->insert("cdkey_type", $rowdata);
            insert_log(translate('MENU_REWARD_CDKEY_TYPE'),translate('CDKEYTYPE_ADD', $rowdata));
            cp_tips("success", $id);
        }
        $one = $this->center_connect->one("select max(type) as type from cdkey_type ");
        $this->view->assign('cdkeytype', $one['type'] + 1);
        $this->view->display("freegift/cdkeytype/add");
    }

    function edit($id = 0) {
        if ($id === 0)
            error(translate('C_INVALID_PARAM_ID', $id));
        $row = $this->center_connect->one("SELECT type,name FROM cdkey_type WHERE type='{$id}'");
        if (!is_array($row))
            error(translate('C_INVALID_PARAM_ID', $id));
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $one = $this->center_connect->one("SELECT type FROM cdkey_type WHERE type='{$rowdata['type']}'");
            if (is_array($one))
                error(translate('C_EXISTED_PARAM_ID', $rowdata['type']));
            //更新数据表
            $result = $this->center_connect->update("cdkey_type", $rowdata, array('type' => $id));
            insert_log(translate('MENU_REWARD_CDKEY_TYPE'),translate('EDIT'), $row, $rowdata);
            cp_tips("success", $id);
        }
        $this->view->display("freegift/cdkeytype/edit", array('row' => $row));
    }

    function del($id = 0) {
        $c = 0;
        $r = '';
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $row = $this->center_connect->one("SELECT type FROM cdkey_type WHERE type='{$id}'");
        if (!is_array($row))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        if ($this->center_connect->delete("cdkey_type", array('type' => $id))) {
            insert_log(translate('MENU_REWARD_CDKEY_TYPE'),translate('DEL'), array('id'=>$id));
            $c = 1;
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        $allids = $ids = explode(',', $ids);
        if (count($ids) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = "'" . implode("','", $ids) . "'";
            if ($type == 'delete') {
                $this->center_connect->query("DELETE FROM cdkey_type WHERE type IN ({$ids})");
                insert_log(translate('MENU_REWARD_CDKEY_TYPE'),translate('MULTI_DEL'), array('ids'=>implode(",", $allids)));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }

        json_output($c, $r);
    }

}
